package com.xy;

import android.annotation.TargetApi;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * Created by Y-bao on 2015/12/17.
 */
public class SDKVersionHelper {


    static VersionHelper versionHelper;

    static {
        int version = Build.VERSION.SDK_INT;
        switch (version) {
            case Build.VERSION_CODES.JELLY_BEAN:
                versionHelper = new JellyBeanHelper();
                break;
            default:
                versionHelper = new BaseHelper();
                break;
        }
    }

    public static void setBackground(View view, Drawable drawable) {
        versionHelper.setBackground(view, drawable);
    }

    public static Drawable getBackground(View view) {
        return versionHelper.getBackground(view);
    }


    public static void removeOnGlobalLayoutListener(ViewTreeObserver viewTreeObserver, ViewTreeObserver.OnGlobalLayoutListener victim) {
        versionHelper.removeOnGlobalLayoutListener(viewTreeObserver, victim);
    }

    public interface VersionHelper {
        void setBackground(View view, Drawable drawable);

        Drawable getBackground(View view);

        void removeOnGlobalLayoutListener(ViewTreeObserver viewTreeObserver, ViewTreeObserver.OnGlobalLayoutListener victim);
    }

    static class BaseHelper implements VersionHelper {

        @Override
        public void setBackground(View view, Drawable drawable) {
            view.setBackgroundDrawable(drawable);
        }

        @Override
        public void removeOnGlobalLayoutListener(ViewTreeObserver viewTreeObserver, ViewTreeObserver.OnGlobalLayoutListener victim) {
            viewTreeObserver.removeGlobalOnLayoutListener(victim);
        }

        @Override
        public Drawable getBackground(View view) {
            return view.getBackground();
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    static class JellyBeanHelper implements VersionHelper {

        @Override
        public void setBackground(View view, Drawable drawable) {
            view.setBackground(drawable);
        }

        @Override
        public void removeOnGlobalLayoutListener(ViewTreeObserver viewTreeObserver, ViewTreeObserver.OnGlobalLayoutListener victim) {
            viewTreeObserver.removeOnGlobalLayoutListener(victim);
        }

        @Override
        public Drawable getBackground(View view) {
            return view.getBackground();
        }
    }

}
